﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using One2Team.EasyScrum.UI.Web.Models;

namespace One2Team.EasyScrum.UI.Web.Controllers
{
    public class ProjectController : Controller
    {
        private easyscrumEntities db = new easyscrumEntities();

        //
        // GET: /Project/

        public ActionResult Index()
        {
            var project = db.project.Include(p => p.product_backlog);
            return View(project.ToList());
        }

        //
        // GET: /Project/Details/5

        public ActionResult Details(int id = 0)
        {
            project project = db.project.Find(id);
            if (project == null)
            {
                return HttpNotFound();
            }
            return View(project);
        }

        //
        // GET: /Project/Create

        public ActionResult Create()
        {
            //ViewBag.id_product_backlog = new SelectList(db.product_backlog, "id", "nom");
            return View();
        }

        //
        // POST: /Project/Create

        [HttpPost]
        public ActionResult Create(project project)
        {
            if (ModelState.IsValid)
            {
                // On créer un ProductBacklog et on le lie au projet.
                var pb = new product_backlog();
                pb.nom = "Product_Backlog_"+project.nom;
                project.product_backlog = pb;

                db.project.Add(project);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            //ViewBag.id_product_backlog = new SelectList(db.product_backlog, "id", "nom", project.id_product_backlog);
            return View(project);
        }

        //
        // GET: /Project/Edit/5

        public ActionResult Edit(int id = 0)
        {
            project project = db.project.Find(id);
            if (project == null)
            {
                return HttpNotFound();
            }
            //ViewBag.id_product_backlog = new SelectList(db.product_backlog, "id", "nom", project.id_product_backlog);
            return View(project);
        }

        //
        // POST: /Project/Edit/5

        [HttpPost]
        //public ActionResult Edit(int id, string nom)
        //public ActionResult Edit(FormCollection form) //form["id"]
        public ActionResult Edit(project project)
        {
            if (ModelState.IsValid)
            {
                //Ici on recuperera via le ProjectRepository le productBacklog associé,
                // et on modifira son nom en fonction du nouveau nom du projet


                db.Entry(project).State = EntityState.Modified;
                //db.Entry(project.product_backlog).State = EntityState.Unchanged;

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            //ViewBag.id_product_backlog = new SelectList(db.product_backlog, "id", "nom", project.id_product_backlog);
            return View(project);
        }

        //
        // GET: /Project/Delete/5

        public ActionResult Delete(int id = 0)
        {
            project project = db.project.Find(id);
            if (project == null)
            {
                return HttpNotFound();
            }
            return View(project);
        }

        //
        // POST: /Project/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            project project = db.project.Find(id);
            db.project.Remove(project);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}